﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class SachDAO
    {
        public bool InsertSach(DAUSACH DauSach, string NhaXuatBan, int NamXB, int SoLuong, int GiaTien)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                //Kiểm tra nếu sách tồn tại thì thêm số lượng
                SACH sach_temp = entity.SACHes.FirstOrDefault(o => o.MaDauSach == DauSach.MaDauSach && o.NhaXB == NhaXuatBan && o.NamXB == NamXB);
                if (sach_temp != null)
                {
                    sach_temp.SoLuong += SoLuong;
                }
                else
                {
                    SACH sach = new SACH()
                    {
                        MaDauSach = DauSach.MaDauSach,
                        NhaXB = NhaXuatBan,
                        NamXB = NamXB,
                        SoLuong = SoLuong,
                        GiaTien = GiaTien
                    };
                    entity.SACHes.AddObject(sach);
                }
                entity.SaveChanges();
                return true;
            }
        }
        private bool UpdateSoLuong(int MaSach, int SoLuongThem)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == MaSach);
                if (sach == null)
                    return false;
                sach.SoLuong += SoLuongThem;
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteSach(DAUSACH DauSach, string NhaXuatBan, int NamXB)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.DAUSACH == DauSach && o.NhaXB == NhaXuatBan && o.NamXB == NamXB);
                if (sach == null)// không tìm thấy
                {
                    return false;
                }

                entity.SACHes.DeleteObject(sach);
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteSach(int MaSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == MaSach);
                if (sach != null)
                {
                    List<CUONSACH> listCS = GetListCuonSach(sach);
                    if(listCS!=null)
                    foreach (CUONSACH cs in listCS)
                    {
                        if (!new CuonSachDAO().DeleteCuonSach(cs.MaCuonSach))
                            return false;
                    }
                    var list = new CTPhieuNhapSachDAO().GetCTPhieuNhapSach(sach.MaSach);
                    foreach (CTPHIEUNHAPSACH item in list)
                    {
                        entity.Attach(item);
                        entity.CTPHIEUNHAPSACHes.DeleteObject(item);
                    }
                    entity.SACHes.DeleteObject(sach);
                    entity.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public SACH GetSach(DAUSACH DauSach, string NhaXuatBan, int NamXB)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaDauSach == DauSach.MaDauSach && o.NhaXB == NhaXuatBan && o.NamXB == NamXB);
                return sach;
            }
        }
        public SACH GetSach(int MaSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == MaSach);
                return sach;
            }
        }

        public DAUSACH GetDauSach(SACH Sach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == Sach.MaSach);
                return sach.DAUSACH;
            }
        }
 
        public THELOAISACH GetTheLoaiSach(SACH Sach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == Sach.MaSach);
                return sach.DAUSACH.THELOAISACH;
            }
        }

        public List<SACH> GetListSach()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<SACH> lSach = entity.SACHes.ToList();
                return lSach;
            }
        }
        public List<TACGIA> GetListTacGia(SACH Sach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == Sach.MaSach);
                return sach.DAUSACH.TACGIAs.ToList();
            }
        }
        public List<CUONSACH> GetListCuonSach(SACH Sach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.MaSach == Sach.MaSach);
                return sach.CUONSACHes.ToList();
            }
        }     
        public List<CUONSACH> GetListCuonSachByTenSach(string TenSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                SACH sach = entity.SACHes.FirstOrDefault(o => o.DAUSACH.TenSach.Equals(TenSach));
                return sach.CUONSACHes.ToList();
            }
        }
        public List<List<TACGIA>> GetListTacGiaByTenSach(string TenSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var listSach = from temp in GetListSach() where GetDauSach(temp).TenSach.Equals(TenSach) select temp;
                List<List<TACGIA>> result = new List<List<TACGIA>>();
                foreach (var item in listSach)
                {
                    if (!result.Contains(GetListTacGia(item)))
                        result.Add(GetListTacGia(item));
                }
                return result;
            }
        }
        public List<string> GetTheLoaiByTenSach(string TenSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var listSach = from temp in GetListSach() where GetDauSach(temp).TenSach.Equals(TenSach) select temp;
                List<string> result = new List<string>();
                foreach (var item in listSach)
                {
                    if (!result.Contains(GetTheLoaiSach(item).TenTheLoaiSach))
                        result.Add(GetTheLoaiSach(item).TenTheLoaiSach);
                }
                return result;
            }
        }
    }
}
